﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using FacebookAcessLibrary.FacebookClass;
using GiveGraphMappers;
using GiveGraphModel;
using WebApp.Models;

namespace WebApp.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Search/

        public ActionResult Index(SearchType searchType, int page)
        {
            switch (searchType)
            {
                case SearchType.Institution:
                    return Institutions(page);
                //break;
                case SearchType.Product:
                    return Products(page);
                //break;
                case SearchType.Service:
                    return Services(page);
                //break;
                default:
                    return null;
            }
        }

        public ActionResult Institutions(int? page)
        {
            if (page.HasValue)
                return View("ShowResults", null);

            return View("SearchInstitutions");
        }

        public ActionResult Volunteers(int? page)
        {
            if (page.HasValue)
                return View("ShowResults", null);

            return View("SearchVolunteers");
        }

        public ActionResult Products(int? page)
        {
            if (page.HasValue)
                return View("ShowResults", null);

            return View("SearchProducts");
        }

        public JsonResult Services(int organization)
        {
            var areaOfVolunteering = TempData["ServiceSearch"] as string[];
            Organization org = new OrganizationMapper().Get(organization);

            ServiceRequestMapper mapper = new ServiceRequestMapper();
            var requests = (from p in mapper.GetAll()
                            where p.Organization.Equals(org) && areaOfVolunteering.Contains(p.Category.Name)
                            select p);

            ServiceMapper mapperSer = new ServiceMapper();
            int count = 0;
            foreach (var request in requests)
            {
                ServiceCategory category1 = request.Category;
                var fittingVolunteers = (from p in mapperSer.GetAll()
                                         where p.Categories.Contains(category1)
                                         select (User)p.ProviderMember);
                count += fittingVolunteers.Count();
                
                var notification = new Notification();
                notification.NotifyUsers(fittingVolunteers.Select(v => v.FacebookToken).Select(v => long.Parse(v)),
                    String.Format("{0} wants you to volunteer!", org.Name),
                    new Uri("http://localhost:50630/Service/RespondServiceRequest?requestId=" + request.ServiceRequestId));
            
            }

            return Json(string.Format("We have notified {0} volunteers about your latest request.",count),JsonRequestBehavior.AllowGet);
        }

    }
}
